package com.caiheng.api.service.wxuser.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.caiheng.api.dao.estate.EstateMapper;
import com.caiheng.api.dao.wxuser.UserAddressMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.estate.Estate;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.entity.wxuser.UserAddress;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.wxuser.UserAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserAddressServiceImpl extends BaseServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Resource
    private UserAddressMapper userAddressMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private EstateMapper estateMapper;

    @Override
    public UserAddress selectDefaultAddress(Integer user_id) {
        QueryWrapper<UserAddress> qw = new QueryWrapper<>();
        qw.eq("is_default",1);
        qw.eq("user_id",user_id);
        qw.last("limit  1");
        return userAddressMapper.selectOne(qw);
    }

    @Override
    public int updateAllAddressToNoDefault(Integer userId) {
        QueryWrapper<UserAddress> qw = new QueryWrapper<>();
        qw.eq("is_default",1);
        qw.eq("user_id",userId);
        List<UserAddress> list = userAddressMapper.selectList(qw);
        if(list != null){
            for(UserAddress address : list){
                userAddressMapper.update(address,
                        new LambdaUpdateWrapper<UserAddress>()
                                .set(UserAddress::getIsDefault, 0)
                                .eq(UserAddress::getId,address.getId()));
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public int addAddress(User user, UserAddress userAddress) {
        try{
            int ret = userAddressMapper.insert(userAddress);
            if(ret <= 0){
                throw new TransactionalException("userAddressMapper insert error");
            }

            if(user.getEstateId() == null){
                user.setEstateId(userAddress.getEstateId());
                Estate estate = estateMapper.selectById(userAddress.getEstateId());
                if(estate!=null){
                    user.setPropertyId(estate.getPropertyId());
                }
            }
            if(user.getPhoneNum() == null){
                user.setPhoneNum(userAddress.getPhoneNum());
            }
            ret = userMapper.updateById(user);
            if(ret <= 0){
                throw new TransactionalException("userMapper updateById error");
            }
            return ret;

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("productOrderMapper insert error");
        }
    }


    @Override
    @Transactional
    public int editAddress(User user, UserAddress userAddress) {
        try{
            int ret = userAddressMapper.updateById(userAddress);
            if(ret <= 0){
                throw new TransactionalException("userAddressMapper insert error");
            }

            if(user.getEstateId() == null){
                user.setEstateId(userAddress.getEstateId());
            }
            if(user.getPhoneNum() == null){
                user.setPhoneNum(userAddress.getPhoneNum());
            }
            ret = userMapper.updateById(user);
            if(ret <= 0){
                throw new TransactionalException("userMapper updateById error");
            }
            return ret;

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("productOrderMapper insert error");
        }
    }
}
